package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/*
类简介：

功能：

时间：2020/4/17

*/
@Service(interfaceClass = FoodService.class)
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodDao foodDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage()<0?1:queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Food> foods;
        if (queryPageBean.getQueryString() != null){
            foods = foodDao.findPage("%" + queryPageBean.getQueryString().trim() + "%");
            return new PageResult(foods.getTotal(),foods.getResult());
        }else {
            foods = foodDao.findPage("%%");
            return new PageResult(foods.getTotal(),foods.getResult());
        }
    }

    @Override
    public void upload(List<String[]> strings) {
        for (String[] string : strings) {

            if (foodDao.isExit(Integer.parseInt(string[0])) > 0){
                foodDao.updateOne(Integer.parseInt(string[0]),string[1],string[2],string[3],string[4]);
            }else {
                foodDao.insertOne(Integer.parseInt(string[0]),string[1],string[2],string[3],string[4]);
            }

        }
    }
}
